/**
 * FileName: PackageAdapter.java
 * @Description: TODO(用一句话描述该文件做什么)
 * All rights Reserved, Designed By 21winmess
 * Copyright: Copyright(C) 2010-2011 
 * Company 21winmess DaLian LTD.
 * @author: Matrix xu
 * @version V2.0
 * Createdate: 2012-12-10 下午4:35:17
 *
 * Modification History:
 * Date Author Version Discription
 * -----------------------------------------------------------------------------------
 * 2012-12-10 Qute_Android2.0
 * Why & What is modified: <修改原因描述>
 */
package com.me.safebox.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalBitmap.BitmapLoader;
import net.tsz.afinal.bitmap.core.BitmapDisplayConfig;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.me.safebox.R;
import com.me.safebox.service.FileScanner.FilePackage;
import com.me.safebox.ui.core.SafeboxAdapter;
import com.me.safebox.ui.photo.FileEncrypt;

/**
 * @ClassName: PackageAdapter
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: Matrix
 * @date: 2012-12-10 下午4:35:17
 * 
 */
public class MediaScanAdapter extends SafeboxAdapter {

	private List<FilePackage> dataList = new ArrayList<FilePackage>();

	private Context mContext = null;

	private FinalBitmap mFinalBitmap = null;

	public MediaScanAdapter(Context context, FinalBitmap finalBitmap) {
		mContext = context;
		mFinalBitmap = finalBitmap;
		mFinalBitmap.registBitmapLoader(mBitmapLoader);
	}

	public void setData(FilePackage filePkg) {
		dataList.add(filePkg);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		return dataList.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		DataHolder dataHolder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_ablum_item, null);
			dataHolder = new DataHolder();
			dataHolder.lableView = (TextView) convertView.findViewById(R.id.ablum_lable_view);
			dataHolder.countView = (TextView) convertView.findViewById(R.id.ablum_count_view);
			dataHolder.imgView = (ImageView) convertView.findViewById(R.id.ablum_first_img_view);
			convertView.setTag(dataHolder);
			clearView.add(convertView);
		} else {
			dataHolder = (DataHolder) convertView.getTag();
		}
		final FilePackage filePkg = (FilePackage) dataList.get(position);
		dataHolder.lableView.setText(filePkg.pkgName);
		dataHolder.countView.setText("(" + filePkg.fileCount + ")");
		//dataHolder.pkgNameView.setText(filePkg.pkgName + "(" + filePkg.fileCount + ")");
		// 同步加载图片
		// Bitmap source = BitmapFactory.decodeFile(filePkg.firstFilePath);
		// Bitmap bm = ThumbnailUtils.extractThumbnail(source, 50, 50);
		// dataHolder.imgView.setImageBitmap(bm);
		// 异步加载图片
		// mImageLoader.loadImage(filePkg.firstFilePath, dataHolder, this);
		mFinalBitmap.display(dataHolder.imgView, filePkg.firstFilePath);
		dataHolder.imgView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(mContext, FileEncrypt.class);
				intent.putExtra("count", filePkg.fileCount);
				intent.putExtra("folderpath", filePkg.pkgAbsPath);
				intent.putExtra("folderName", filePkg.pkgName);
				mContext.startActivity(intent);
			}
		});

		return convertView;
	}

	public class DataHolder {
		public ImageView imgView = null;
		public TextView lableView = null;
		public TextView countView = null;

	}

	@Override
	public void clearView() {
		for (View view : clearView) {
			if (view == null || view.getTag() == null) {
				continue;
			}
			DataHolder vh = (DataHolder) view.getTag();
			vh.imgView.setImageBitmap(null);
			vh.lableView.setText(null);
			vh.countView.setText(null);
		}
		clearView.clear();
		//mFinalBitmap.unregistBitmapLoader();
	}

	@Override
	public void clearData() {
		dataList.clear();
	}
	
	private BitmapLoader mBitmapLoader = new BitmapLoader() {
		@Override
		public Bitmap processBitmap(String uri, BitmapDisplayConfig config) {
			return mFinalBitmap.proccessBitmapFromStores(uri, config);
		}
	};
}
